package com.kuangstudy.thread.demo2;

import com.kuangstudy.thread.demo1.Demo2;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.net.URL;
import java.util.concurrent.*;

/**
 * @author Losca
 * @date 2021/7/19 14:14
 * 线程创建方式三 实现callable接口
 */
public class Demo implements Callable<Boolean> {

    private String url;
    private String name;
    @Override
    public Boolean call() throws Exception {
        WebDownLoader webDownLoader = new WebDownLoader();
        webDownLoader.down(url,name);

        return true;
    }
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Demo2 thread1 = new Demo2("https://pics0.baidu.com/feed/810a19d8bc3eb135d6c55dc2bc7478dbfd1f4438.png?token=f9945350eb0262bdc5c918a4226a6494","1.jpg");
        Demo2 thread2 = new Demo2("https://pics5.baidu.com/feed/eac4b74543a98226c07c15d69fe869094b90eb70.png?token=f8f06d22232baa4549feb031457b29e6","2.jpg");
        Demo2 thread3 = new Demo2("https://pics1.baidu.com/feed/060828381f30e924c2c113a75b62be0e1c95f7b6.png?token=99d1ae2589b2215251746664a4a853c8","3.jpg");
        /*创建服务*/
        ExecutorService ser = Executors.newFixedThreadPool(3);
        /*提交服务*/
        Future<?> r1 = ser.submit(thread1);
        Future<?> r2 = ser.submit(thread2);
        Future<?> r3 = ser.submit(thread3);
        /*获取结果*/
        Object rs1 = r1.get();
        Object rs2 = r2.get();
        Object rs3 = r3.get();
        /*关闭服务*/
        ser.shutdown();
    }

}

class WebDownLoader{
    public void down(String url,String name) throws Exception {
        FileUtils.copyURLToFile(new URL(url),new File(name));
    }
}
